<?php
/**
 * Created by PhpStorm.
 * User: mrjnamei
 * Date: 17-5-12
 * Time: 上午10:52
 */

namespace App\Winged\Patients\Api;

use Carbon\Carbon;

class Kase extends Api
{
    /**
     * 接口
     * @var array
     */
    public static $actions = [
        // 获取病例列表
        'getCaseList'               => 'doctor/patient/case?index=%s&number=%s&mine=%s',
        // 获取病例总条数
        'getCaseCount'              => 'doctor/patient/case/count?mine=%s',
        // 获取某患者的病例列表
        'getPatientCaseList'        => 'doctor/patient/case?patient_id=%s&index=%s&number=%s&mine=%s',
        //　获取某患者的病例总数
        'getPatientCaseCount'       => 'doctor/patient/case/count?patient_id=%s&mine=%s',
        //获取某患者病例的入院记录
        'getPatientRuyuanCaseList'  => 'doctor/patient/hospitalized?case_id=%s&index=%s&number=%s',
        // 获取某患者病例的入院记录总数
        'getPatientRuyuanCaseCount' => 'doctor/patient/hospitalized/count?case_id=%s',
        //给某患者添加新病例 POST
        'addNewPatientCase'         => 'doctor/patient/case',
        //获取病例类别 默认１０００条数据
        'getDiseaseCategoryList'    => 'doctor/patient/disease/category?index=0&number=1000',
        //创建入院记录 POST
        'addNewRuyuan'              => 'doctor/patient/hospitalized',
        //获取病例详情
        'getPatientCaseDetail'      => 'doctor/patient/case/%s',
        //修改患者病例 PUT
        'modifyPatientCase'         => 'doctor/patient/case',
        //删除患者的一条病例 DELETE
        'deletePatientCase'         => 'doctor/patient/case?case_id=%s',
        //通过通过诊断名称获取相关诊断名称列表
        'getDiagnosesName'          => 'doctor/patient/diagnoses/name?diagnoses_name=%s&index=%s&number=%s',
        //搜索没有诊断名称 就添加输入的诊断名称
        'addDiagnosesName'          => 'doctor/patient/diagnoses/name?index=0&number=100',
        // 搜索某个名称的病例条数
        'getDiagnosesNameCount'     => 'doctor/patient/case/count?case_name=%s',
        // 搜索某个病例的名称列表
        'getCaseName'               => 'doctor/patient/case?case_name=%s&index=%s&number=%s',
        // top5
        'top5'                      => 'doctor/statistic?type=%s',


    ];

    /**
     * 获取病例列表
     * @return $data = [
     *      'data' => Collection ,
     *      'count' => [] ,
     * ];
     */
    public static function makeList($patient_id, $currentPage, $currentPageSize, $mine = 0)
    {
        if (is_null($patient_id)) {
            $data  = self::doGet('getCaseList', $currentPage, $currentPageSize, $mine);
            $count = self::doGet('getCaseCount', $mine);
        } else {
            $data  = self::doGet('getPatientCaseList', $patient_id, $currentPage, $currentPageSize,$mine);
            $count = self::doGet('getPatientCaseCount', $patient_id,$mine);
        }
        $data = $data->map(function ($item, $index) {
            if (!array_key_exists('lastSurgeryDate', $item['properties'])) {
                $item['properties']['lastSurgeryDate'] = '';
            }
            if (!array_key_exists('lastHospitalizeDate', $item['properties'])) {
                $item['properties']['lastHospitalizeDate'] = '';
            }
            return $item;
        });
        return array_merge(['data' => $data], $count->toArray());
    }

    public static function getCaseName($name, $currentPage, $currentPageSize)
    {
        $data  = self::doGet('getCaseName', $name, $currentPage, $currentPageSize);
        $count = self::doGet('getDiagnosesNameCount', $name);
        $data  = $data->map(function ($item, $index) {
            if (!array_key_exists('lastSurgeryDate', $item['properties'])) {
                $item['properties']['lastSurgeryDate'] = '';
            }
            if (!array_key_exists('lastHospitalizeDate', $item['properties'])) {
                $item['properties']['lastHospitalizeDate'] = '';
            }
            return $item;
        });
        return array_merge(['data' => $data], $count->toArray());
    }

    /**
     * 创建新的病例
     */
    public static function addNewPatientCase($data)
    {
        $res = self::doPost('addNewPatientCase', $data);
        return $res;
    }

    /*
     * 获取病例详情
     */
    public static function getCaseDetail($id)
    {
        $res = self::doGet('getPatientCaseDetail', $id);
        return $res;
    }

    /**
     * 获取所有的病例名称列表
     */
    public static function addDiagnosesName()
    {
        return self::doGet('addDiagnosesName');
    }

    /**
     * @param $id 病例id
     * 删除病例
     */
    public static function deleteCase($id)
    {
        return self::doDelete('deletePatientCase', $id);
    }

    /**
     * 修改病例
     */
    public static function updateCase($data)
    {
        return self::doPut('modifyPatientCase', $data);
    }

    /**
     * 获取top5
     */
    /**
     * @param array $type
     * //0top5诊断 1top5手术名称
     * //2患者年龄统计 3有无随访 4月病例数 5月入院数
     */
    public static function getTop5($type)
    {
        return self::doGet('top5' ,$type);
    }

}